package com.example.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.SearchCodePO;
import com.example.bean.UserPO;
import com.example.dto.SearchParamDTO;
import com.example.dto.SearchResultDTO;
import com.example.mapper.mysql.mysqlMapper;
import com.example.mapper.starrocks.StarRocksMapper;
import com.example.service.DataService;
import com.example.util.ExceptionUtil;
import com.example.util.LogUtil;
import com.example.util.SqlThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/data")
public class DataController {


    mysqlMapper mysqlMapper;

    StarRocksMapper starRocksMapper;

    DataService dataService;

    public DataController(mysqlMapper mysqlMapper, StarRocksMapper starRocksMapper, DataService dataService) {
        this.mysqlMapper = mysqlMapper;
        this.starRocksMapper = starRocksMapper;
        this.dataService = dataService;
    }


    @GetMapping("/list1")
    public List<SearchCodePO> getList1() {
        return mysqlMapper.selectList(null);
    }

    @GetMapping("/list2")
    public List<UserPO> getList2() {
        return starRocksMapper.selectList(null);
    }

    @PostMapping("/searchData")
    public SearchResultDTO searchData(@RequestBody SearchParamDTO searchParamDTO) throws Exception {
        log.info(LogUtil.getCurrentMethodPath() + "收到查询数据请求调用#参数{}", JSONUtil.toJsonStr(searchParamDTO));
        if (StrUtil.isBlank(searchParamDTO.getSqlCode())) {
            throw new Exception("sqlCode不能为空");
        }
        String sqlContent = dataService.selectSql(searchParamDTO.getSqlCode());
        if (StrUtil.isBlank(sqlContent)) {
            throw new Exception("sql配置不存在");
        }
        try {
            //设置SQL参数
            SqlThreadLocalUtil.setSqlContent(sqlContent);
            //组装分页参数
            Page page = new Page();
            page.setCurrent(searchParamDTO.getCurrent());
            page.setSize(searchParamDTO.getSize());
            //执行查询
            IPage resultPage = dataService.searchData(page, searchParamDTO.getSearchParam());
            //封装结果
            SearchResultDTO searchResultDTO = new SearchResultDTO();
            searchResultDTO.setCurrent((int) resultPage.getCurrent());
            searchResultDTO.setSize((byte) resultPage.getSize());
            searchResultDTO.setTotal(resultPage.getTotal());
            searchResultDTO.setRecords(resultPage.getRecords());
            log.info(LogUtil.getCurrentMethodPath() + "请求完成返回结果#{}", JSONUtil.toJsonStr(resultPage.getRecords().size() > 5 ? resultPage.getRecords().size() : resultPage.getRecords()));
            return searchResultDTO;
        } catch (Throwable throwable) {
            log.error(LogUtil.getCurrentMethodPath() + "查询数据出现异常#异常信息#{}", JSONUtil.toJsonStr(ExceptionUtil.getStackTrace(throwable)));
            throw throwable;
        } finally {
            SqlThreadLocalUtil.removeAll();
        }

    }

    /**
     * @param searchParamDTO
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
   /* @PostMapping("/getData")
    public String searchData(@RequestBody SearchParamDTO searchParamDTO) throws Exception {
        String sql = dataService.selectSql(searchParamDTO.getSqlCode());
        if (StrUtil.isBlank(sql)) {
            throw new Exception("sql配置不存在");
        }
        String script = SCRIPT_PREFIX + sql + SCRIPT_SUFFIX;

        SqlSessionFactory sqlSessionFactory = SpringUtil.getBean(SQL_SESSION_FACTORY_BEAN_NAME, SqlSessionFactory.class);
        Configuration configuration = sqlSessionFactory.getConfiguration();
        LanguageDriverRegistry languageRegistry = configuration.getLanguageRegistry();

        SqlSource sqlSource = languageRegistry.getDriver(XMLLanguageDriver.class).createSqlSource(configuration, script, searchParamDTO.getSearchParam().getClass());
        MappedStatement mappedStatement = configuration.getMappedStatement(MAPPER_METHOD_NAME);
        Field field = mappedStatement.getClass().getDeclaredField(SQL_SOURCE_FILED_NAME);
        field.setAccessible(true);
        field.set(mappedStatement, sqlSource);

        Page page = new Page();
        page.setCurrent(searchParamDTO.getCurrent());
        page.setSize(searchParamDTO.getSize());
        IPage resultPage = starRocksMapper.getData(page, searchParamDTO.getSearchParam());

        SearchResultDTO searchResultDTO = new SearchResultDTO();
        searchResultDTO.setCurrent((int) resultPage.getCurrent());
        searchResultDTO.setSize((byte) resultPage.getSize());
        searchResultDTO.setTotal(resultPage.getTotal());
        searchResultDTO.setRecords(resultPage.getRecords());
        return JSONUtil.toJsonStr(searchResultDTO);

    }*/


}
